package com.dyh.quickdemo.demos.lbdbean;

import com.dyh.quickdemo.bizDemo.syncWarehouseToWms.Action;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.function.Consumer;

/*
@Author=Joey Wheeler
@Date=2020/12/30 13:37
*/
public class CsMacro<T> {
    String name;

    LinkedList<Consumer<T>> consumers;
    //Map<Integer,Macro> fallbackMap;


    public static <T>  CsMacro<T> init(String name){
        CsMacro<T> macro = new CsMacro<>();
        macro.name = name;
        macro.consumers = new LinkedList<>();
        return macro;
    }

    public CsMacro<T> plan(Consumer<T>... actions){
        this.consumers.addAll(Arrays.asList(actions));
        return this;
    }

//    public Macro fallback(int indx,Macro macro){
//        this.fallbackMap.put(indx,macro);
//        return this;
//    }

    public LinkedList<Consumer<T>> run(T t){

        Consumer<T> consumer;
        /*peek null 支持*/
        while ( (consumer = consumers.peek()) !=null){
            try {
                consumer.accept(t);
            } catch (Exception e) {
                return consumers;
            }
            consumers.poll();//null支持
        }
        return null;
    }
}
